package com.pureman.androidapp;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.mikepenz.materialdrawer.app.DrawerActivity;
import com.nightonke.wowoviewpagerexample.WoWoViewPagerActivity;
import com.pureman.androidapp.appintro.AppIntroActivity;
import com.pureman.androidapp.avloadingindicator.AVLoadingIndicatorActivity;
import com.pureman.androidapp.exception.ExceptionActivity;
import com.pureman.androidapp.holocolorpicker.HoloColorPickerActivity;
import com.pureman.androidapp.imagepicker.ImagePickerActivity;
import com.pureman.androidapp.material_animations.MaterialAnimationsActivity;
import com.pureman.androidapp.ninegridimageview.NineGridImageViewActivity;
import com.pureman.androidapp.numberprogressbar.NumberProgressbarActivity;
import com.pureman.androidapp.pickerview.PickerViewActivity;
import com.pureman.androidapp.titanic.TitanicActivity;
import com.pureman.androidapp.utils.LogUtils;
import com.sothree.slidinguppanel.demo.SlidinguppanelActivity;

import mr_immortalz.com.stereoview.StereoViewMainActivity;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    private ListView listView;
    private String[] explains;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        LogUtils.i("MainActivity-->onCreate");

        initView();
        setData();
    }

    private void initView() {
        listView = (ListView) findViewById(R.id.listView);
        //注册监听事件
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView parent, View view,
                                    int position, long id) {
                String string = (String) parent.getItemAtPosition(position);
                switch (position) {
                    case 0:
                        startActivity(new Intent(MainActivity.this, ExceptionActivity.class));
                        break;
                    case 1:
                        startActivity(new Intent(MainActivity.this, StereoViewMainActivity.class));
                        break;
                    case 2:
                        startActivity(new Intent(MainActivity.this, PickerViewActivity.class));
                        break;
                    case 3:
                        startActivity(new Intent(MainActivity.this, AVLoadingIndicatorActivity.class));
                        break;
                    case 4:
                        startActivity(new Intent(MainActivity.this, HoloColorPickerActivity.class));
                        break;
                    case 5:
                        startActivity(new Intent(MainActivity.this, ImagePickerActivity.class));
                        break;
                    case 6:
                        startActivity(new Intent(MainActivity.this, NumberProgressbarActivity.class));
                        break;
                    case 7:
                        startActivity(new Intent(MainActivity.this, TitanicActivity.class));
                        break;
                    case 8:
                        startActivity(new Intent(MainActivity.this, WoWoViewPagerActivity.class));
                        break;
                    case 9:
                        startActivity(new Intent(MainActivity.this, NineGridImageViewActivity.class));
                        break;
                    case 10:
                        startActivity(new Intent(MainActivity.this, AppIntroActivity.class));
                        break;
                    case 11:
                        startActivity(new Intent(MainActivity.this, MaterialAnimationsActivity.class));
                        break;
                    case 12:
                        startActivity(new Intent(MainActivity.this, DrawerActivity.class));
                        break;
                    case 13:
                        startActivity(new Intent(MainActivity.this, SlidinguppanelActivity.class));
                        break;
                }
                Toast.makeText(MainActivity.this, explains[position], Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void setData() {
        explains = getResources().getStringArray(R.array.explains);
        ArrayAdapter arrayAdapter = new ArrayAdapter(
                this, android.R.layout.simple_list_item_1,
                explains);
        listView.setAdapter(arrayAdapter);
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    private boolean mIsExit;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (mIsExit) {
                this.finish();
            } else {
                Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show();
                mIsExit = true;
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mIsExit = false;
                    }
                }, 2000);
            }
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }
}
